﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Net.Mail;
using System.Data;
using System.IO;
using System.Xml;
using System.Collections;

namespace lcmyjobs.Web
{
    public class myPublic
    {
        public static void sendMail(string strto, string subj, string bodys)
        {
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            _smtpClient.Host = "smtp.sina.com.cn";//指定SMTP服务器
            _smtpClient.Credentials = new System.Net.NetworkCredential("haoafei@sina.com", "8");//用户名和密码

            MailMessage _mailMessage = new MailMessage("haoafei@sina.com", strto);
            _mailMessage.Subject = subj;//主题
            _mailMessage.Body = bodys;//内容
            _mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码
            _mailMessage.IsBodyHtml = true;//设置为HTML格式
            _mailMessage.Priority = MailPriority.High;//优先级
            _smtpClient.Send(_mailMessage);
        }
        public static string SafetyStr(string theString)
        {
            string[] aryReg = { "'", ";", "\"", "\r", "\n" };
            for (int i = 0; i < aryReg.Length; i++)
            {
                theString = theString.Replace(aryReg[i], string.Empty);
            }
            return theString;
        }
        public static string GetIp()
        {
            string ip;
                string[] temp;
                bool isErr = false;
                if (HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
                    ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
                else
                    ip = HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
                if (ip.Length > 15)
                    isErr = true;
                else
                {
                    temp = ip.Split('.');
                    if (temp.Length == 4)
                    {
                        for (int i = 0; i < temp.Length; i++)
                        {
                            if (temp[i].Length > 3) isErr = true;
                        }
                    }
                    else
                        isErr = true;
                }

                if (isErr)
                    return "1.1.1.1";
                else
                    return ip;
        }
        public static void Modify(string key, string strvalue)
        {
            string xpath = "/myconfiguration/appSet/add[@key='?']";
            XmlDocument domwebconfig = new XmlDocument();

            domwebconfig.Load(HttpContext.Current.Server.MapPath("/myconfig.xml"));
            XmlNode addkey = domwebconfig.SelectSingleNode((xpath.Replace("?", key)));
            if (addkey == null)
            {
                throw new ArgumentException("没有找到<add key=" + key + " value=.../>的配置节");
            }
            addkey.Attributes["value"].InnerText = strvalue;
            domwebconfig.Save(HttpContext.Current.Server.MapPath("/myconfig.xml"));
        }
        public static string GetConfig(string key)
        {
            string xpath = "/myconfiguration/appSet/add[@key='?']";
            XmlDocument domwebconfig = new XmlDocument();

            domwebconfig.Load(HttpContext.Current.Server.MapPath("/myconfig.xml"));
            XmlNode addkey = domwebconfig.SelectSingleNode((xpath.Replace("?", key)));
            if (addkey == null)
            {
                throw new ArgumentException("没有找到<add key=" + key + " value=.../>的配置节");
            }
            return addkey.Attributes["value"].InnerText ;
            //domwebconfig.Save(HttpContext.Current.Server.MapPath("/web.config"));
            
        }
        public static string GetMd5(string md5str)
        {            
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(md5str, "MD5").Substring(8, 16).ToLower();           
            
        }
      
    }
}